﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StarcraftDemo4
{
    class GameOptimizer
    {
        public static void PlayBaseGames(int maxNum)
        {
            int GameId;
            string filename;
            for (int i = 0; i < maxNum; i++)
            {
                Game TestGame = new Game();
                AutoPlayer Scott = new AutoPlayer();
                TestGame.PlayAutoGame(Scott);
                //movelist is the name of the file.
                filename = i + "dat";

                Recorder.SerializeMoves(TestGame.MovesPlayed, filename);

                Recorder.SendToDB_EF(Scott.myState, out GameId, filename);
                //GameId = i;
                Console.WriteLine("***{0}***", GameId);
            }
        }
        public static void PlayOverSelectedGame(List<Move> SelectedGameMoves, int NumMoves, out Game VariantGame, out AutoPlayer Scott)
        {
            //this returns a Game and a player who played through
            //a single variant of the first NumMoves of SelectedGameMoves
            //it also stores its variant game to the DB.
            //can use:
            //List<Move> MovesofGame = GameOptimizer.GrabGameMovesFromDB(oldGameId);
            //to get SelectedGamesMoves.

            Game BaseGame = new Game();
            VariantGame = new Game();
            Scott = new AutoPlayer();
            int GameId;
            //need to keep a pristine copy of Selected Games.
            Move[] CopyofSGM = Game.DeepClone<Move[]>(SelectedGameMoves.ToArray());
            Move[] SubArrayOfSGM = new Move[NumMoves];
            Array.Copy(CopyofSGM, 0, SubArrayOfSGM, 0, NumMoves);

            BaseGame.ReplayGame(Scott, SubArrayOfSGM);

            //play rest of this game from there.
            VariantGame.MovesPlayed.AddRange(BaseGame.MovesPlayed);
            VariantGame.PlayAutoGame(Scott);

            Recorder.SerializeAndSendToDB(VariantGame, Scott.myState, out GameId);
            Console.WriteLine("*********{0}**********", GameId);
        }

        public static List<Move> GrabGameMovesFromDB(int GameId)
        {
            string filename = Recorder.GetBinaryFileFromDB_EF(GameId);
            List<Move> myRecoveredGameMoves = (List<Move>)Recorder.DeserializeMoves(filename);
            File.Delete(filename);
            return myRecoveredGameMoves;
        }
        public static int MaxNum(Units_Name maxUnit)
        {
            using (SC2GamesResultsEntities DB = new SC2GamesResultsEntities())
            {
                switch (maxUnit)
                {
                    case (Units_Name.Marine):
                        return DB.UnitsTbls.Max<UnitsTbl>(x => x.Marines);                        
                    case (Units_Name.Marauder):
                        return DB.UnitsTbls.Max<UnitsTbl>(x => x.Marauders);
                    case (Units_Name.SCV):
                        return DB.UnitsTbls.Max<UnitsTbl>(x => x.SCVs);                        
                    case (Units_Name.Ghost):
                        return DB.UnitsTbls.Max<UnitsTbl>(x => x.Ghosts);
                    default:
                        return DB.UnitsTbls.Max<UnitsTbl>(x => x.Marines);
                        
                }
            }
        }
        public static int MaxGameId(Units_Name maxUnit)
        {
            using (SC2GamesResultsEntities DB = new SC2GamesResultsEntities())
            {
                int MaxNumber, result;
                switch (maxUnit)
                {
                    case(Units_Name.Marine):
                        MaxNumber = DB.UnitsTbls.Max<UnitsTbl>(x => x.Marines);
                        var seta =
                            from u in DB.UnitsTbls
                            where u.Marines == MaxNumber
                            select u.GameId;
                        result = (int)seta.FirstOrDefault();
                        break;
                    case (Units_Name.SCV):
                        MaxNumber = DB.UnitsTbls.Max<UnitsTbl>(x => x.SCVs);
                        var sets =
                            from u in DB.UnitsTbls
                            where u.SCVs == MaxNumber
                            select u.GameId;
                        result = (int)sets.FirstOrDefault();
                        break;
                    case (Units_Name.Marauder):
                        MaxNumber = DB.UnitsTbls.Max<UnitsTbl>(x => x.Marauders);
                        var setd =
                            from u in DB.UnitsTbls
                            where u.Marauders == MaxNumber
                            select u.GameId;
                        result = (int)setd.FirstOrDefault();
                        break;
                    default:
                        MaxNumber = DB.UnitsTbls.Max<UnitsTbl>(x => x.SCVs);
                        var sets2 =
                            from u in DB.UnitsTbls
                            where u.SCVs == MaxNumber
                            select u.GameId;
                        result = (int)sets2.FirstOrDefault();
                        break;
                }
                return result;
            }
        }
    }
}
